#include "relationlabel.h"

RelationLabel::RelationLabel()
{
    this->setGraphicsEffect(0);
    this->setFlags(QGraphicsItem::ItemIsSelectable);

    this->addToGroup(&this->_borderItem);

    this->_textItem.setFont(this->smallFont());
    this->addToGroup(&this->_textItem);

    this->setZValue(std::numeric_limits<int>::min());
    this->_borderItem.setZValue(std::numeric_limits<int>::min());
    this->_textItem.setZValue(std::numeric_limits<int>::min());
}

void RelationLabel::setText(QString name)
{
    this->_textItem.setText(name);
    this->updateBoundingbox();
}

void RelationLabel::setBrush(QBrush brush)
{
    this->_borderItem.setBrush(brush);
    this->_borderItem.setPen(QPen(brush.color()));
    this->updateBoundingbox();
}

void RelationLabel::updateBoundingbox()
{
    QRectF rect = this->_textItem.boundingRect();

    this->_boundingRect = rect.adjusted(-3, 0, 3, 0);

    QPainterPath path;
    path.addRoundedRect(this->_boundingRect, 3, 3);

    this->_borderItem.setPos(0, 0);
    this->_borderItem.setPath(path);

    this->_textItem.setPos(0, 0);
}

void RelationLabel::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
    emit this->labelDoubleClick(event);
}

QRectF RelationLabel::boundingRect() const
{
    return this->_boundingRect;
}
